package com.example.myapplicationndk.h264;

import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaFormat;
import android.util.Log;
import android.view.Surface;

import androidx.annotation.Nullable;

import java.nio.ByteBuffer;

public class H264EncodeThread extends Thread {

    private MediaFormat mediaFormat;
    private MediaCodec mediaCodec;
    private boolean isStop = false;
    private byte[] data;


    public H264EncodeThread() {

    }

    public Surface initMediaCodec(int WIDTH, int HEIGHT, @Nullable byte[] data) {
        int width = WIDTH;
        int height = HEIGHT;

        if ((WIDTH & 1) == 1) {
            width = WIDTH - 1;
        }
        if ((HEIGHT & 1) == 1) {
            height = HEIGHT - 1;
        }

        width = 720;
        height = 1080;

        if (data != null) {
            this.data = data;
        }

        Surface surface = null;
        try {
            Log.d("TAGG-1", width + " " + height);

            mediaCodec = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_VIDEO_AVC);

            mediaFormat = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, width, height);
            // 帧率
            mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 60);
            //  I 帧间隔
            mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 60);
            //  码率
            mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, width * height * 4);
            // 重要
            mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);

            // 配置解码器
            mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);

            surface = mediaCodec.createInputSurface();

            return surface;

        } catch (Exception e) {
            Log.d("TAGGG1", e.toString());
        }
        return surface;
    }

    @Override
    public void run() {
        super.run();
        try {
            if (mediaCodec != null) {
                Log.d("TAGG1", Thread.currentThread().getName());
                mediaCodec.start();
                MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
                while (!isStop) {

                    if (data != null) {
                        int inIndex = mediaCodec.dequeueInputBuffer(100_000);
                        if (inIndex >= 0) {
                            ByteBuffer byteBuffer = mediaCodec.getInputBuffer(inIndex);
                            byteBuffer.clear();
                            byteBuffer.put(data);
                            mediaCodec.queueInputBuffer(
                                    inIndex,
                                    0,
                                    data.length,
                                    System.nanoTime() / 1000,
                                    0
                            );
                        }
                    }


//直接拿到输出，不用管输入，输入已经被实现了
                    int outIndex = mediaCodec.dequeueOutputBuffer(info, 100_000);
                    if (outIndex >= 0) {
//                编码的数据
                        ByteBuffer byteBuffer = mediaCodec.getOutputBuffer(outIndex);
                        byte[] ba = new byte[byteBuffer.remaining()];
                        byteBuffer.get(ba);//将容器的byteBuffer  内部的数据 转移到 byte[]中
                        FileUtils.writeBytes(ba);
                        FileUtils.writeContent(ba);
                        mediaCodec.releaseOutputBuffer(outIndex, false);

                    }
                }

            } else {
                Log.d("TAGG2", Thread.currentThread().getName());
                Log.d("TAGGG", "null mediacodec");
            }

        } catch (Exception e) {
            Log.d("TAGGG", e.toString());
        } finally {
            mediaCodec.stop();
            mediaCodec.release();
        }

    }


    public void startEncode() {
        isStop = false;
        start();
    }

    public void stopEncode() {
        isStop = true;
    }
}
